package com.taugin.wechatreader.presenter;

import android.os.Handler;

import com.taugin.wechatreader.business.IBusiness;
import com.taugin.wechatreader.business.OnWxReaderListener;
import com.taugin.wechatreader.business.WxReaderBusiness;
import com.taugin.wechatreader.info.WxContactInfo;
import com.taugin.wechatreader.iview.IView;
import com.taugin.wechatreader.utils.Log;

import java.util.List;

/**
 * Created by ck01- on 2017/6/15.
 */

public class WxReaderPresenter {

    private IView mView;
    private IBusiness mBusiness;
    private Handler mHandler;

    public WxReaderPresenter(IView view) {
        mView = view;
        mHandler = new Handler();
        mBusiness = new WxReaderBusiness();
        mBusiness.setContext(mView.getContext());
        mBusiness.setOnWxReaderListener(mOnWxReaderListner);
    }

    public void connectDatabase() {
        mBusiness.checkEnv();
    }

    public void destroy() {
        mBusiness.onDestroy();
    }

    private OnWxReaderListener mOnWxReaderListner = new OnWxReaderListener() {
        @Override
        public void onInitSuccess() {
            Log.d(Log.TAG, "");
            mBusiness.connectDatabase();
        }

        @Override
        public void onInitFailure(String error) {
            Log.d(Log.TAG, "error : " + error);
        }

        @Override
        public void onReadContact(final List<WxContactInfo> list) {
            Log.d(Log.TAG, "");
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mView.onReadContact(list);
                    mView.hideProgress();
                }
            });
        }

        @Override
        public void error(final String msg) {
            Log.d(Log.TAG, "");
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mView.hideProgress();
                    mView.showErrorMsg(msg);
                }
            });
        }

        @Override
        public void onConnectDBStart() {
            Log.d(Log.TAG, "");
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mView.showProgress();
                }
            });
        }

        @Override
        public void onConnectDBEnd(boolean success) {
            Log.d(Log.TAG, "success : " + success);
            if (success) {
                mBusiness.readContacts();
            } else {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mView.hideProgress();
                        mView.showErrorMsg("数据库连接失败");
                    }
                });
            }
        }

        @Override
        public void showProgressMsg(final String msg) {
            Log.d(Log.TAG, "msg : " + msg);
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mView.updateProgress(msg);
                }
            });
        }
    };

}
